<?php

namespace app\user\controller;

use app\BaseController;
use app\model\KefuQuickReply;
use app\Request;
use app\user\service\KefuService;
use app\user\service\UserService;

/**
 * 客服基本
 * Class Index
 * @package app\controller\kefu
 * @author dgc<453342213@qq.com>
 * createTime:2020/8/24 11:54
 */
class Index extends BaseController
{
    /**
     * 获取详情
     * @param Request $request
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 11:54
     */
    public function info(Request $request)
    {
        try {
            returnDate($request->userInfo);
        } catch (\Exception $e) {
            returnMsg($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取聊天对话
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 12:04
     */
    public function chatList(Request $request)
    {
        try {
            $res = (new KefuService())->chatList($request->userInfo);
            returnDate($res);
            new KefuService();
        } catch (\Exception $e) {
            returnMsg($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取聊天对话
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 12:04
     */
    public function chatLogList(Request $request)
    {
        try {
            $data = $this->checkParam(['chat_id'], false);
            $res = (new UserService())->chatLogList($request->userInfo, $data);
            returnDate($res);
            new KefuService();
        } catch (\Exception $e) {
            returnMsg($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加快捷回复
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/27 17:32
     */
    public function addQuickReply(Request $request)
    {
        try {
            $data = $this->checkParam(['content'], false);
            $res = (new KefuQuickReply())->save(['kefu_id' => $request->userInfo['id'], 'content' => $data['content']]);
            if ($res) {
                returnMsg('添加成功');
            }
            returnMsg('添加成功', -1);
            new KefuService();
        } catch (\Exception $e) {
            returnMsg($e->getMessage(), $e->getCode());
        }
    }

    /**
     *  获取快捷回复列表
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/27 17:31\
     */
    public function quickReplyList(Request $request)
    {
        try {
            $res = (new KefuQuickReply())->where(['kefu_id' => $request->userInfo['id']])
                ->field('content')
                ->order('id desc')
                ->select();
            returnDate($res);
            returnMsg('删除成功');
            new KefuService();
        } catch (\Exception $e) {
            returnMsg($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除快捷回复
     * @param Request $request
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/27 17:48
     */
    public function delQuickReply(Request $request)
    {
        try {
            $data = $this->checkParam(['reply_id'], false);
            $res = (new KefuQuickReply())->where(['id' => $data['reply_id'], 'kefu_id' => $request->userInfo['id']])->delete();
            if ($res) {
                returnMsg('删除成功');
            }
            returnMsg('删除失败', -1);
        } catch (\Exception $e) {
            returnMsg($e->getMessage(), $e->getCode());
        }
    }
}
